global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/table_A11_autopauto.log, replace name(tabl)

capture noi {
    * v9

    * ------------------------------------------- *
    * ------------------------------------------- *
    * Auto Pauto (direction of innov regressions)
    * ------------------------------------------- *
    * ------------------------------------------- *

    qui do ${code_dir}/config/tabletools.do
    use ${final_dir}/regression_dataset_from1970_tfacit1.dta, clear

    * configuration
    global pdef "pauto90"


    * restrict to baseline firms
    mmerge BvD using ${final_dir}/bvd_list_regfirms_auto95.dta, unmatched(master)
    keep if _m == 3

    tab year, gen(YY_)
    clonevar LSW = lswMPm_1995_a
    clonevar HSW = hswMPm_1995_a
    clonevar VAEMP = vaempMPm_1995_a
    clonevar GDPGAP = lngdpgap_1995_a
    clonevar GDPPC = gdppcMPm_1995_a

    clonevar GDPPC_foreign = gdppcMPm_shr4_foreign_1995_a
    clonevar VAEMP_foreign = vaempMPm_shr4_foreign_1995_a
    clonevar GDPgap_foreign = lngdpgap_shr_foreign_1995_a
    clonevar LSW_foreign = lswMPm_shr4_foreign_1995_a
    clonevar HSW_foreign = hswMPm_shr4_foreign_1995_a

    clonevar stockauto = k${depvar}_${ttt}
    clonevar stockautozero = k${depvar}_${ttt}0  
    clonevar stocknonauto = k${pdef}_${ttt}
    clonevar stocknonautozero = k${pdef}_${ttt}0
    clonevar stocknonmach = kNOT_tfa_${ttt}
    clonevar stocknonmachzero = kNOT_tfa_${ttt}0    
    clonevar spilloversauto = spill${depvar}${ttt}_1995_a
    clonevar spilloversautozero = spill${depvar}${ttt}_1995_a0
    clonevar spilloversnonauto = spill${pdef}${ttt}_1995_a
    clonevar spilloversnonautozero = spill${pdef}${ttt}_1995_a0
    clonevar spilloversnonmach = spillNtfa${ttt}_1995_a
    clonevar spilloversnonmachzero = spillNtfa${ttt}_1995_a0


    clonevar stockown = k${depvar}_${ttt}
    clonevar stockownzero = k${depvar}_${ttt}0  
    clonevar stockother = kNOT_${depvar}_${ttt} 
    clonevar stockotherzero = kNOT_${depvar}_${ttt}0
    clonevar spilloversown = spill${depvar}${ttt}_1995_a
    clonevar spilloversother = spillN${depvar}${ttt}_1995_a
    clonevar spilloversownzero = spill${depvar}${ttt}_1995_a0
    clonevar spilloversotherzero = spillN${depvar}${ttt}_1995_a0

    local stock stockown stockownzero stockother stockotherzero
    local spill spilloversown spilloversownzero spilloversother spilloversotherzero
    local stock2 stockauto stockautozero stocknonauto stocknonmach stocknonmachzero
    local spill2 spilloversauto spilloversautozero spilloversnonauto spilloversnonautozero spilloversnonmach spilloversnonmachzero

    bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+2 & year <= 2009+2
    bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
    drop _total_${depvar}_${ttt}_1995

    egen yearctry = group(year country_shr_1995) if year <= 2009
    egen yearindustry = group(year industry) if year <= 2009
    egen yearctryindustry = group(year industry country_shr_1995) if year <= 2009
    sort lse_id year


    * -------------------------------- *
    * Arcsinh configurations
    * -------------------------------- *

    estimates clear

    * use arcsinh(pauto) as control
    gen arcsinh_${pdef} = log(${pdef}_${ttt}+((${pdef}_${ttt})^2+1)^(1/2))
    gen F2_arcsinh_${pdef} = F2.arcsinh_${pdef}
    *1) Arcsinh pauto90 | F + IY
    ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP `stock' `spill' F2_arcsinh_${pdef} if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry)
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\no"
    hasvar stock* spill*, local(stockspill)
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col_1

    *4) Arcsinh pauto90 | F + IY + CY
    ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP `stock' `spill' F2_arcsinh_${pdef} if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\yes"
    hasvar stock* spill*, local(stockspill)
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col_4
    
    *7) Arcsinh pauto90 | F + IY + CY | foreign
    ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign VAEMP_foreign `stock' `spill' F2_arcsinh_${pdef} if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry yearctry)
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\yes"
    hasvar stock* spill*, local(stockspill)
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col_7


    * -------------------------------- *
    * Dummy control configurations
    * -------------------------------- *

    * pauto90 and LSW + HSW
    * ------------------ *
    replace ${pdef}_${ttt} = 0 if missing(${pdef}_${ttt})
    gen log${pdef} = ln(${pdef}_${ttt})
    replace log${pdef} = 0 if ${pdef}_${ttt} == 0
    gen ${pdef}_${ttt}N0 = (${pdef}_${ttt} > 0)
    clonevar any${pdef} = ${pdef}_${ttt}N0

    gen F2_any${pdef} = F2.any${pdef}
    gen F2_log${pdef} = F2.log${pdef}
    *2) Log pauto90 + dummy | F + IY
    ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP `stock' `spill' F2_log${pdef} F2_any${pdef} if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry)
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\no"
    hasvar stock* spill*, local(stockspill)
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col_2
    *5) Log pauto90 + dummy | F + IY + CY
    ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP `stock' `spill' F2_log${pdef} F2_any${pdef} if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\yes"
    hasvar stock* spill*, local(stockspill)
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col_5
    *8) Log pauto90 + dummy | F + IY + CY | foreign
    ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign VAEMP_foreign `stock' `spill' F2_log${pdef} F2_any${pdef} if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry yearctry)
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\yes"
    hasvar stock* spill*, local(stockspill)
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col_8

    
    * -------------------------------- *
    * Offset pauto specifications
    * -------------------------------- *

    gen ln_${pdef}_${ttt} = log(${pdef}_${ttt})
    gen F2_ln_${pdef}_${ttt} = F2.ln_${pdef}_${ttt}



    * Offset + dummy
    * ------------------ *

    replace F2_ln_${pdef}_${ttt} = 0 if missing(F2_ln_${pdef}_${ttt})
    *3) pauto90 | F + IY
    ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP `stock' `spill' F2_any${pdef} if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id) offset(F2_ln_${pdef}_${ttt}) absorb(lse_id yearindustry)
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\no"
    hasvar stock* spill*, local(stockspill)
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col_3
    *6) pauto90 | F + IY + CY
    ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP `stock' `spill' F2_any${pdef} if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id) offset(F2_ln_${pdef}_${ttt}) absorb(lse_id yearindustry yearctry)
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\yes"
    hasvar stock* spill*, local(stockspill)
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col_6
    *9) pauto90 | F + IY + CY | foreign
    ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign VAEMP_foreign `stock' `spill' F2_any${pdef} if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id) offset(F2_ln_${pdef}_${ttt}) absorb(lse_id yearindustry yearctry)
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\yes"
    hasvar stock* spill*, local(stockspill)
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col_9


    * -------------------------------- *
    * Output tables
    * -------------------------------- *

    * Output main text table
    do ${code_dir}/config/tabletools.do
    setlabels, sname("own")
    dhoztab col_1 col_2 col_3 col_4 col_5 col_6 col_7 col_8 col_9 using ${tab_dir}/appendix/Table_A11_autopauto.tex, b(%10.2f) se(%10.2f) nogaps replace numbers nonotes nolines nomtitles sfmt(a1) scalars("has_stockspill \noalign{\medskip} Stocks and spillovers" "f Firm fixed effects" "iy Industry \stimes year fixed effects" "cy Country \stimes year fixed effects" "obs \noalign{\medskip} Observations"  "firms Number of firms") label mgroups("Domestic and foreign" "Foreign", pattern(1 0 0 0 0 0  1 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) drop(*zero stock* spill* _cons) noobs rename(LSW_foreign LSW HSW_foreign HSW GDPgap_foreign GDPGAP VAEMP_foreign VAEMP GDPPC_foreign GDPPC F2_logpauto90 Logpauto90 F2_anypauto90 Anypauto90 F2_arcsinh_pauto90 Arcsinhpauto90) ///
        depvar("Auto95") ///
        notes("This table shows regressions with a control for non-automation machinery innovations (pauto90). Columns 1, 4, and 7 control for the arcsinh of pauto90 patent flow. Columns 2, 5, and 8 control for log pauto90 and a dummy variable indicating at least 1 pauto90 innovation. Columns 3, 6, and 9 constrain the coefficient on log pauto90 to 1. Standard errors are clustered at the firm-level and reported in parentheses.")
    *again, } brackets
}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}